package com.hancher.gamelife2.colockin;

import android.os.Bundle;

import androidx.navigation.Navigation;
import androidx.sqlite.db.SimpleSQLiteQuery;

import com.hancher.common.androidutil.AsyncUtils;
import com.hancher.common.androidutil.ToastUtil;
import com.hancher.common.base.BaseFragment;
import com.hancher.common.rv.PageRecyclerViewManager;
import com.hancher.gamelife2.R;
import com.hancher.gamelife2.databinding.FragmentColockInSignBinding;
import com.hancher.gamelife2.main.MainDatabase_Impl;
import com.lxj.xpopup.XPopup;

import java.util.Date;
import java.util.List;

import io.reactivex.rxjava3.functions.Consumer;

/**
 * 描述 : 打卡界面 <br/>
 * 时间 : 2024/4/12 15:11 <br/>
 * 作者 : Hancher ytu_shaoweijie@163.com
 */
public class ColockInSignFragment extends BaseFragment<FragmentColockInSignBinding> {


    private PageRecyclerViewManager<ColockInTypePO> prvm;

    @Override
    protected void initView(Bundle savedInstanceState) {
        prvm = new PageRecyclerViewManager<ColockInTypePO>()
                .setContext(getContext())
                .setRecyclerView(binding.recyclerview)
                .setAdapter(new ColockInSignAdapter())
                .setDivider(true)
                .setCol(5)
                .setOnItemClickListener((baseQuickAdapter, view, i) -> {
                    ColockInTypePO colockInTypePO = prvm.getRvList().get(i);
                    new XPopup.Builder(getContext())
                            .asCustom(new ColockInSignDialog(getContext(), colockInTypePO, (CharSequence description, Date selectDate) -> {
                                AsyncUtils.run(emitter -> {
                                    ColockInPO colockIn = new ColockInPO();
                                    colockIn.setTypeUuid(colockInTypePO.getUuid());
                                    colockIn.setDescription(description.toString());
                                    if (selectDate != null) {
                                        colockIn.setUpdateTime(selectDate.getTime());
                                    }
                                    // 新增打卡记录
                                    MainDatabase_Impl.getInstance().colockInDao().insertItem(colockIn);
                                    // 重新统计总计
                                    long count = MainDatabase_Impl.getInstance().colockInDao().count(colockIn.getTypeUuid());
                                    //更新类型总计
                                    colockInTypePO.setCount(count);
                                    MainDatabase_Impl.getInstance().colockInTypeDao().updateItem(colockInTypePO);
                                    emitter.onNext("打卡成功");
                                    emitter.onComplete();
                                }, (Consumer<String>) s -> {
                                    ToastUtil.show(s);
                                    queryList();
                                });
                            }))
                            .show();
                })
                .setRefreshLayout(binding.refreshLayout)
                .setEnableRefresh(false)
                .setEnableLoadMore(false)
                .init();
        binding.imgEdit.setOnClickListener(v -> Navigation.findNavController(binding.container).navigate(R.id.action_colockInMainFragment_to_colockInEditFragment));
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initListener() {
    }

    @Override
    public void onResume() {
        super.onResume();
        queryList();
    }

    private void queryList() {
        AsyncUtils.run(emitter -> {
            String sql = " SELECT * FROM colockintype WHERE deleteflag!=1 ";
            List<ColockInTypePO> list = MainDatabase_Impl.getInstance().colockInTypeDao().getList(new SimpleSQLiteQuery(sql));
            emitter.onNext(list);
        }, (Consumer<List<ColockInTypePO>>) data -> {
            prvm.freshData(data);
        });
    }

}